using System;
using System.Linq;
using EzDesk.Apps.Model.Staff;
using EzDesk.Apps.ServiceImpl.Staff.Repositories.Interface;

namespace EzDesk.Apps.ServiceImpl.Staff.Repositories
{
    public partial class StaffInfoRepository : IContactInfoRepository
    {
        EzDeskStaffDataContext _db;

        public StaffInfoRepository(EzDeskStaffDataContext db)
        {
            this._db = db;
        }

        #region IRepository<StaffInfo> Members

        public Guid Add(ContactInfo model, bool saveChanges)
        {
            _db.ez_StaffInfos.InsertOnSubmit(
                new ez_StaffInfo()
                {
                    StaffInfoId = model.Id,
                    UserId = model.UserId,
                    QqNumber = model.QqNumber,
                    MobileNo = model.MobileNo,
                    MainEmail = model.MainEmail,
                });
            if (saveChanges)
            {
                this.SaveChanges();
            }
            return model.Id;
        }

        public ContactInfo GetById(Guid id)
        {
            ContactInfo info = null;

            info = _db.ez_StaffInfos.Where(p => p.StaffInfoId == id)
                .Select(p => new ContactInfo()
                                 {
                                     Id = p.StaffInfoId,

                                     UserId = p.UserId,
                                     QqNumber = p.QqNumber,
                                     MobileNo = p.MobileNo,
                                     MainEmail = p.MainEmail,
                                 })
                .SingleOrDefault();

            return info;
        }

        public IQueryable<ContactInfo> GetAll()
        {
            return _db.ez_StaffInfos.Select(
                p => new ContactInfo()
                {
                    Id = p.StaffInfoId,

                    UserId = p.UserId,
                    QqNumber = p.QqNumber,
                    MobileNo = p.MobileNo,
                    MainEmail = p.MainEmail,
                }
            );
        }

        public void Delete(Guid id, bool saveChanges)
        {
            ez_StaffInfo entity = _db.ez_StaffInfos.Where(p => p.StaffInfoId == id).SingleOrDefault();
            if (entity != null)
            {
                _db.ez_StaffInfos.DeleteOnSubmit(entity);
            }
            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void Update(ContactInfo model, bool saveChanges)
        {
            var entity = _db.ez_StaffInfos.Where(p => p.StaffInfoId == model.Id)
                .SingleOrDefault();

            entity.UserId = model.UserId;
            entity.QqNumber = model.QqNumber;
            entity.MobileNo = model.MobileNo;
            entity.MainEmail = model.MainEmail;

            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void SaveChanges()
        {
            _db.SubmitChanges();
        }
        #endregion
    }
}
